<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MY_Controller {

    function __construct () {
        parent::__construct();
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['title']    = 'ATTO 管理機能';
        $this->load->model('admin_model');
        $this->load->helper(array('form','url'));
    }

    public function index(){
        $this->view('admin/login',$this->data);
    }

    public function auth(){
        $params['login_name'] = $this->input->post('admin_name');
        $params['login_pass'] = MY_Helper::generateHashString($this->input->post('password'));
        if(!empty($params['login_name']) && !empty($params['login_pass'])){
            if($id = $this->admin_model->checkAdmin($params)) {
                $this->session->set_userdata('logined','OK');
                $this->session->set_userdata('user_login',array('login_name' => $params['login_name'], 'id' => $id));
                redirect('/admin/main/','refresh');
                exit;
            }
        }
        $this->data['msg'] = "IDまたはパスワードが正しくありません。";
        $this->view('admin/login',$this->data);
    }

    public function logout(){
        $this->session->sess_destroy();
        redirect('/admin/');
        exit;
    }
}
